package server

import (
	"errors"
	"fmt"
	"math/rand"
	"videozg3/common/models/mysql"
	"videozg3/common/models/redis"
	user "videozg3/common/proto"
)

func SendSms(in *user.SendSmsReq) (*user.SendSmsResp, error) {

	get := redis.RedisGet("SendSmsIncr" + in.Mobile)
	if get >= 1 {
		return nil, errors.New("60 秒内只能获取一次验证码")
	}

	code := rand.Intn(9000) + 1000
	redis.RedisSet("SendSms"+in.Source+in.Mobile, code)
	incr := redis.RedisIncr("SendSmsIncr" + in.Mobile)
	if incr == 1 {
		redis.RedisExpire("SendSmsIncr" + in.Mobile)
	}
	return &user.SendSmsResp{Greet: "发送成功"}, nil
}
func UserLogin(in *user.UserLoginReq) (*user.UserLoginResp, error) {
	get := redis.RedisGet("SendSms" + "login" + in.Mobile)
	if get != int(in.Code) {
		return nil, errors.New("验证码错误")
	}

	u := &mysql.User{}
	mobile, err := u.FindUserByMobile(in.Mobile)
	fmt.Println(err)
	if err != nil {
		return nil, errors.New("手机号查询失败")
	}
	if mobile.ID == 0 {
		createUser, err := mobile.CreateUser(in.Mobile)
		if err != nil {
			return nil, errors.New("创建失败")
		}
		mobile = createUser
	}
	defer redis.RedisDel("SendSms" + "login" + in.Mobile)
	return &user.UserLoginResp{Greet: int64(mobile.ID)}, nil
}
func VideoList(in *user.VideoListReq) (*user.VideoListResp, error) {
	v := &mysql.Video{}
	page := in.Page
	if page < 1 {
		page = 1
	}
	pageSize := in.PageSiz
	if pageSize < 10 {
		pageSize = 10
	}
	offset := (page - 1) * pageSize
	videos, err := v.FindVideos(offset, pageSize)
	if err != nil {
		return nil, err
	}
	var Item []*user.VideoList
	for _, m := range videos {
		Item = append(Item, &user.VideoList{
			Title:        m.Title,
			Desc:         m.Desc,
			UserId:       m.UserId,
			MusicId:      m.MusicId,
			LikesCount:   m.LikesCount,
			CommentCount: m.CommentCount,
		})
	}
	return &user.VideoListResp{List: Item}, nil
}

func UserCenter(in *user.UserCenterReq) (*user.UserCenterResp, error) {
	u := &mysql.User{}
	by, err := u.FindUserBy(in.UserId)
	fmt.Println(in.UserId)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	fmt.Println(by)
	var Item []*user.UserCenter

	for _, m := range by {
		Item = append(Item, &user.UserCenter{
			Name:    m.Name,
			Age:     m.Age,
			Sign:    m.Sign,
			Xingzuo: m.Sign,
			Sex:     m.Sex,
		})
		fmt.Println(m)
	}
	fmt.Println(Item)
	return &user.UserCenterResp{Item: Item}, nil
}
func AddVideo(in *user.AddVideoReq) (*user.AddVideoResp, error) {
	v := &mysql.Video{}
	err := v.AddVideo(in)
	if err != nil {
		return nil, err
	}
	return &user.AddVideoResp{Greet: "发送成功"}, nil
}

func UserLike(in *user.UserLikeReq) (*user.UserLikeResp, error) {
	v := &mysql.Video{}
	video, err := v.FindVideo(in.WorkId)
	if err != nil {
		return nil, err
	}
	count := video.LikesCount + 1
	err = video.UpdateLikeCount(in.WorkId, int(count))
	if err != nil {
		return nil, err
	}
	return &user.UserLikeResp{Greet: "点赞成功"}, nil
}

func CancelUserLike(in *user.CancelUserLikeReq) (*user.CancelUserLikeResp, error) {
	v := &mysql.Video{}
	video, err := v.FindVideo(in.WorkId)
	if err != nil {
		return nil, err
	}
	count := video.LikesCount - 1
	err = video.UpdateLikeCount(in.WorkId, int(count))
	if err != nil {
		return nil, err
	}
	return &user.CancelUserLikeResp{Greet: "点赞成功"}, nil
}
